package com.kedsoft.andaily.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.kedsoft.andaily.common.CommonResult;
import com.kedsoft.andaily.entity.Datum;
import com.kedsoft.andaily.service.DatumService;
import com.kedsoft.andaily.vo.PageVo;
import com.kedsoft.andaily.vo.Pagination;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api("资料下载管理")
@RestController
@RequestMapping(value = "/api/datum")
public class DatumApiController {
    @Autowired
    private DatumService datumService;

    @ApiOperation(value = "资料下载")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "page", dataType = "int",defaultValue = "1", required = true, value = "第几页"),
            @ApiImplicitParam(paramType = "query", name = "rows", dataType = "int",defaultValue = "20", required = true, value = "行数"),
            @ApiImplicitParam(paramType = "query", name = "sort", dataType = "String", defaultValue = "id", required = false, value = "排序主键"),
            @ApiImplicitParam(paramType = "query", name = "order", defaultValue = "asc", dataType = "String", required = false, value = "排序"),
    })
    @RequestMapping(value = "pageList", method = RequestMethod.GET)
    public CommonResult<PageVo<Datum>> getPageList(Pagination pagination)
    {
        EntityWrapper<Datum> entityWrapper = new EntityWrapper<>();
        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<Datum> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());

        datumService.selectPage(page,entityWrapper);
        PageVo<Datum> pageVo = new PageVo<>(page.getRecords(),page.getTotal());
        return CommonResult.success(pageVo);
    }
}
